<template>
  <base-layout
    dialog-title="x"
    :dialog-visible="dialogVisible"
    dialog-width="600px"
    :page-show="pageShow"
    @dialog-close="dialogClose"
  >
    <template #toolPanel />
    <template #searchPanel>
      <search-panel :show-add-button="false" :show-search-button="true" @generateBill="generateBill"
                    labelWidth="73" @search="search" :left="true" :showGenerateBill="true">
        <el-form-item label="渠道商:" prop="useruuid">
          <el-select v-model="filters.useruuid"  placeholder="请选择渠道商" clearable filterable>
            <el-option value="0" label="全部">全部</el-option>
            <el-option
              v-for="item in users"
              :key="item.user.useruuid"
              :label="item.user.username"
              :value="item.user.useruuid">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="开始时间:">
          <el-date-picker type="date" placeholder="开始时间" format="yyyy-MM-dd" clearable v-model="filters.rechargetime"
                          style="width: 100%;"></el-date-picker>
        </el-form-item>
        <el-form-item label="结束时间:">
          <el-date-picker type="date" placeholder="结束时间" format="yyyy-MM-dd" clearable v-model="filters.endtime"
                          style="width: 100%;"></el-date-picker>
        </el-form-item>
      </search-panel>
    </template>
    <template #contentPanel>
      <data-table
        :value="tableData"
        :total-count="totalCount"
        :has-action="true"
        :current="start"
        :table-column-attributes="tableColumnAttributes"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      >
        <template #action="{row}">
          <el-button size="small" round @click="openFile(row)">下载
          </el-button>
        </template>
      </data-table>
    </template>

    <template #dialog>
    </template>

  </base-layout>
</template>

<script>
import baseMixin from '@/mixins/base'
import util from '../../../utils/util'
import {queryBillUp,generateBillUp} from '@/api/upapi'
import {findAllUpShopInfo} from '@/api/utilsapi'


export default {
  name: 'downbillUp',
  mixins: [
    baseMixin({
      idName: 'id',
      findData:queryBillUp,
      format(res) {
        return res.map(data => {
          return data
        })
      }
    })
  ],
  data() {
    return {
      pageShow: true,
      dialogData:{},
      users:[],
      tableColumnAttributes: [
        { name: '渠道商名称', value: 'name' , formatter : this.userNameFormatter},
        { name: '创建时间', value: 'createTime' },
      ],
      filters: {
        username: '',
        rechargeamount: '',
        rechargetime: '',
        endtime:'',
        useruuid: ''
      }
    }
  },
  methods: {
    async search(form){
      let para = {
        start: 0,
        pageSize: 10,
        starttime: this.filters.rechargetime = (!this.filters.rechargetime || this.filters.rechargetime == '') ? '' : util.formatDate.format(new Date(this.filters.rechargetime), 'yyyy-MM-dd'),
        endtime:this.filters.endtime = (!this.filters.endtime || this.filters.endtime == '') ? '' : util.formatDate.format(new Date(this.filters.endtime), 'yyyy-MM-dd'),
        useruuid: this.filters.useruuid == "0" ? "" :this.filters.useruuid,
        fileType:"BILL"
      };
      this.searchOptions = para;
      this.setTableData()
    },

    async findAllUserShopInfo(){
      let para = {useruuid:''};
      const result = await findAllUpShopInfo(para);
      this.users = result.data;
    },
    openFile(row){
      window.open(row.path);
    },
    async generateBill(){
      let para = {useruuid:this.filters.useruuid == 0 ? "" : this.filters.useruuid,
        starttime: this.filters.rechargetime = (!this.filters.rechargetime || this.filters.rechargetime == '') ? '' : util.formatDate.format(new Date(this.filters.rechargetime), 'yyyy-MM-dd'),
        endtime:this.filters.endtime = (!this.filters.endtime || this.filters.endtime == '') ? '' : util.formatDate.format(new Date(this.filters.endtime), 'yyyy-MM-dd')};
      const result = await generateBillUp(para);
      if(result.code == "0"){
        this.showMsg('对账单生成成功，稍后刷新查看',"info");
      }
    }
  },
  mounted() {
    this.findAllUserShopInfo();
  }
}
</script>
